package controllers

import (
	"LoveU/models"
	"LoveU/utils"
	"fmt"
	"strconv"

	"github.com/astaxie/beego"
)

const AVATARPATH = "static/avatar/"
const ALBUMPATH = "alumb/"
const AVATARURL = "http://120.24.168.102:8080/static/avatar/"
const TMPURL = "http://120.24.168.102:8080/static/tmp/"
const TMPPATH = "static/tmp/"
const PUBLICPATH = "static/photo/"
const HOSTPHOTO = "http://120.24.168.102:8080/static/photo/"

type FileController struct {
	beego.Controller
}

// 上传用户的头像
func (this *FileController) UpLoadAvatar() {
	var codejson interface{}

	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	userid := mySession.Get("userid")

	f, h, err := this.GetFile("avatar")
	if err != nil {
		fmt.Println("getfile err ", err)
	} else {
		filePath := AVATARPATH + h.Filename
		err = this.SaveToFile("avatar", filePath) // 保存位置在 avatar/[userid], 没有文件夹要先创建
		if err != nil {
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
			this.Data["json"] = codejson
			this.ServeJSON()
			return
		}
		f.Close()

		useridStr := strconv.FormatInt(userid.(int64), 10)

		// 修改用户上传文件的名字
		filetype := picType(filePath, AVATARPATH, userid.(int64))
		switch filetype {
		case utils.UNKNOWN_ERROR:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		case utils.ALBUM_DELETE_FAILED:
			codejson = utils.Message{utils.ALBUM_DELETE_FAILED, "删除文件失败", nil, ""}
		case utils.ALBUM_RENAME_FAILED:
			codejson = utils.Message{utils.ALBUM_RENAME_FAILED, "重命名失败", nil, ""}
		case utils.ALBUM_UPLOAD_JPEG:
			URL := AVATARURL + useridStr + ".jpg"
			// 将头像url写入到数据库
			codejson = utils.Message{utils.ALBUM_UPLOAD_JPEG, "success", URL, ""}
		case utils.ALBUM_UPLOAD_GIF:
			URL := AVATARURL + useridStr + ".gif"
			// 将头像url写入到数据库
			codejson = utils.Message{utils.ALBUM_UPLOAD_GIF, "success", URL, ""}
		case utils.ALBUM_UPLOAD_PNG:
			URL := AVATARURL + useridStr + ".png"
			// 将头像url写入到数据库
			codejson = utils.Message{utils.ALBUM_UPLOAD_PNG, "success", URL, ""}
		case utils.ALBUM_UPLOAD_FORMAT_DENIED:
			codejson = utils.Message{utils.ALBUM_UPLOAD_FORMAT_DENIED, "不兼容的图片类型", nil, ""}
		}
		this.Data["json"] = codejson
		this.ServeJSON()
		return
	}
}

func (this *FileController) ModifyAvatar() {
	var codejson interface{}

	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	userid := mySession.Get("userid")

	f, h, err := this.GetFile("avatar")
	if err != nil {
		fmt.Println("getfile err ", err)
	} else {
		filePath := AVATARPATH + h.Filename
		err = this.SaveToFile("avatar", filePath) // 保存位置在 avatar/[userid], 没有文件夹要先创建
		if err != nil {
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
			this.Data["json"] = codejson
			this.ServeJSON()
			return
		}
		f.Close()

		useridStr := strconv.FormatInt(userid.(int64), 10)

		// 修改用户上传文件的名字
		filetype := picType(filePath, AVATARPATH, userid.(int64))
		switch filetype {
		case utils.UNKNOWN_ERROR:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		case utils.ALBUM_DELETE_FAILED:
			codejson = utils.Message{utils.ALBUM_DELETE_FAILED, "删除文件失败", nil, ""}
		case utils.ALBUM_RENAME_FAILED:
			codejson = utils.Message{utils.ALBUM_RENAME_FAILED, "重命名失败", nil, ""}
		case utils.ALBUM_UPLOAD_JPEG:
			URL := AVATARURL + useridStr + ".jpg"
			// 将头像url写入到数据库
			status := models.WriteToAvatar(userid.(int64), URL)
			if status != utils.OK {
				codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
				return
			}
			codejson = utils.Message{utils.ALBUM_UPLOAD_JPEG, "success", URL, ""}
		case utils.ALBUM_UPLOAD_GIF:
			URL := AVATARURL + useridStr + ".gif"
			status := models.WriteToAvatar(userid.(int64), URL)
			if status != utils.OK {
				codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
				return
			}
			// 将头像url写入到数据库
			codejson = utils.Message{utils.ALBUM_UPLOAD_GIF, "success", URL, ""}
		case utils.ALBUM_UPLOAD_PNG:
			URL := AVATARURL + useridStr + ".png"
			status := models.WriteToAvatar(userid.(int64), URL)
			if status != utils.OK {
				codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
				return
			}
			// 将头像url写入到数据库
			codejson = utils.Message{utils.ALBUM_UPLOAD_PNG, "success", URL, ""}
		case utils.ALBUM_UPLOAD_FORMAT_DENIED:
			codejson = utils.Message{utils.ALBUM_UPLOAD_FORMAT_DENIED, "不兼容的图片类型", nil, ""}
		}
		this.Data["json"] = codejson
		this.ServeJSON()
		return
	}
}

// 上传用户的
func (this *FileController) UpLoadAlumb() {
	var codejson interface{}
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	userid := mySession.Get("userid")

	f, h, err := this.GetFile("alumb")
	if err != nil {
		fmt.Println("getfile err ", err)
	} else {
		filePath := ALBUMPATH + h.Filename
		this.SaveToFile("alumb", filePath) // 保存位置在 static/upload, 没有文件夹要先创建
		f.Close()

		// 修改用户上传文件的名字
		// 先建立相应的文件夹
		createAlumb(userid.(int64))
		filetype := picTypeAlumb(filePath, ALBUMPATH, userid.(int64))

		switch filetype {
		case utils.UNKNOWN_ERROR:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		case utils.ALBUM_DELETE_FAILED:
			codejson = utils.Message{utils.ALBUM_DELETE_FAILED, "删除文件失败", nil, ""}
		case utils.ALBUM_RENAME_FAILED:
			codejson = utils.Message{utils.ALBUM_RENAME_FAILED, "重命名失败", nil, ""}
		case utils.ALBUM_UPLOAD_JPEG:
			codejson = utils.Message{utils.ALBUM_UPLOAD_JPEG, "success", nil, ""}
		case utils.ALBUM_UPLOAD_GIF:
			codejson = utils.Message{utils.ALBUM_UPLOAD_GIF, "success", nil, ""}
		case utils.ALBUM_UPLOAD_PNG:
			codejson = utils.Message{utils.ALBUM_UPLOAD_PNG, "success", nil, ""}
		case utils.ALBUM_UPLOAD_FORMAT_DENIED:
			codejson = utils.Message{utils.ALBUM_UPLOAD_FORMAT_DENIED, "不兼容的图片类型", nil, ""}
		default:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		}
		this.Data["json"] = codejson
		this.ServeJSON()
	}
}

// 如何设置用户上传的相册文件的图片的名字
// 将用户的上传完成的系统时间作为相册图片的名字，保持图片类型不变
// 将图片url存入到数据库

func (this *FileController) GetAlbum() {
	var codejson interface{}

	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	userid := mySession.Get("userid")

	// 获取相册图片的参数
	albumURL := this.Input().Get("alumb")
	beego.Error(albumURL)

	status := models.CheckAlbumURL(userid.(int64), albumURL)
	switch status {
	case utils.ALBUM_FILE_NOT_EXIST:
		codejson = utils.Message{utils.ALBUM_FILE_NOT_EXIST, "不存在此文件", "", ""}
	case utils.ALBUM_FILE_EXISTS:
		useridStr := strconv.FormatInt(userid.(int64), 10)
		albumPath := ALBUMPATH + useridStr + "/" + albumURL
		CopyFile(albumPath, TMPPATH+albumURL)
		// systemTime := time.Now()
		// beego.Error(systemTime.Hour())
		// beego.Error(systemTime.Minute())
		// beego.Error(systemTime.Month())
		// beego.Error(systemTime.Day())
		// beego.Error(systemTime.Month())
		// beego.Error(systemTime.Weekday())
		tmpURL := TMPURL + albumURL
		codejson = utils.Message{0, "success", tmpURL, ""}
	default:
		codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", "", ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return

}

// 获取好友照片链接
func (this *FileController) GetFriendAlbum() {
	var codejson interface{}

	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	// userid := mySession.Get("userid")

	// 获取相册图片的参数
	albumURL := this.Input().Get("alumb")
	beego.Error(albumURL)
	friendid := this.Input().Get("friendid")
	fid_int64, err := strconv.ParseInt(friendid, 10, 64)
	if err != nil {
		getJSONFile(this, utils.UNKNOWN_ERROR, "参数错误", "", "")
	}

	status := models.CheckAlbumURL(fid_int64, albumURL)
	switch status {
	case utils.ALBUM_FILE_NOT_EXIST:
		codejson = utils.Message{utils.ALBUM_FILE_NOT_EXIST, "不存在此文件", "", ""}
	case utils.ALBUM_FILE_EXISTS:
		useridStr := strconv.FormatInt(fid_int64, 10)
		albumPath := ALBUMPATH + useridStr + "/" + albumURL
		CopyFile(albumPath, TMPPATH+albumURL)
		// systemTime := time.Now()
		// beego.Error(systemTime.Hour())
		// beego.Error(systemTime.Minute())
		// beego.Error(systemTime.Month())
		// beego.Error(systemTime.Day())
		// beego.Error(systemTime.Month())
		// beego.Error(systemTime.Weekday())
		tmpURL := TMPURL + albumURL
		codejson = utils.Message{0, "success", tmpURL, ""}
	default:
		codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", "", ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

//返回全部相册信息
func (this *FileController) GetAlbums() {
	var codejson interface{}

	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	userid := mySession.Get("userid")

	albums := models.GetALBUMList(userid.(int64))
	if albums != nil {
		codejson = utils.Message{0, "success", albums, ""}
	} else {
		codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", "", ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

//获取好友相册
func (this *FileController) GetFriendsAlbums() {
	var codejson interface{}

	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	userid := mySession.Get("userid")
	friendid := this.Input().Get("friendid")

	fid_int64, err := strconv.ParseInt(friendid, 10, 64)
	if err != nil {
		getJSONFile(this, utils.UNKNOWN_ERROR, "参数错误", "", "")
	}

	flag := models.CheckIsFriend(userid.(int64), fid_int64)
	if flag == false {
		getJSONFile(this, utils.UNKNOWN_ERROR, "对方不是你的好友", "", "")
	}
	albums := models.GetALBUMList(fid_int64)
	if albums != nil {
		codejson = utils.Message{0, "success", albums, ""}
	} else {
		codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", "", ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

// 获取用户的所有好友之头像
func (this *FileController) GetAvatar() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}
	userid := mySession.Get("userid")

	avatar := models.GetFriendsAvatar(userid.(int64))
	if avatar == nil {
		getJSONFile(this, utils.UNKNOWN_ERROR, "数据库错误", "", nil)
	} else {
		getJSONFile(this, utils.OK, "success", "", avatar)
	}
}

// 用户上传图片
func (this *FileController) UploadPhoto() {
	var codejson interface{}

	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginFile(this)
		return
	}

	f, h, err := this.GetFile("photo")
	if err != nil {
		fmt.Println("getfile err ", err)
	} else {
		filePath := PUBLICPATH + h.Filename
		err = this.SaveToFile("photo", filePath) // 保存位置在 avatar/[userid], 没有文件夹要先创建
		if err != nil {
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
			this.Data["json"] = codejson
			this.ServeJSON()
			return
		}
		f.Close()

		filetype, filename := picTypeAll(filePath, PUBLICPATH)
		beego.Error(filetype, filename)

		switch filetype {
		case utils.UNKNOWN_ERROR:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		case utils.ALBUM_DELETE_FAILED:
			codejson = utils.Message{utils.ALBUM_DELETE_FAILED, "删除文件失败", nil, ""}
		case utils.ALBUM_RENAME_FAILED:
			codejson = utils.Message{utils.ALBUM_RENAME_FAILED, "重命名失败", nil, ""}
		case utils.ALBUM_UPLOAD_JPEG:
			codejson = utils.Message{utils.ALBUM_UPLOAD_JPEG, "success", HOSTPHOTO + filename, ""}
		case utils.ALBUM_UPLOAD_GIF:
			codejson = utils.Message{utils.ALBUM_UPLOAD_GIF, "success", HOSTPHOTO + filename, ""}
		case utils.ALBUM_UPLOAD_PNG:
			codejson = utils.Message{utils.ALBUM_UPLOAD_PNG, "success", HOSTPHOTO + filename, ""}
		case utils.ALBUM_UPLOAD_FORMAT_DENIED:
			codejson = utils.Message{utils.ALBUM_UPLOAD_FORMAT_DENIED, "不兼容的图片类型", nil, ""}
		default:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		}
		this.Data["json"] = codejson
		this.ServeJSON()
	}
}
